package com.hexiang.client;

import com.github.lianjiatech.retrofit.spring.boot.annotation.Intercept;
import com.github.lianjiatech.retrofit.spring.boot.annotation.OkHttpClientBuilder;
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.degrade.Degrade;
import com.github.pagehelper.PageInfo;
import com.hexiang.annotation.Sign;
import com.hexiang.domain.User;
import com.hexiang.interceptor.TimeStampInterceptor;
import okhttp3.OkHttpClient;
import retrofit2.http.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RetrofitClient(baseUrl = "http://localhost:8080",fallbackFactory = UserHttpDegradeFallbackFactory.class)
@Intercept(handler = TimeStampInterceptor.class, include = {"/user/**"}, exclude = "/user/savePerson")
@Sign(accessKeyId = "${test.accessKeyId}", accessKeySecret = "${test.accessKeySecret}", exclude = {"/api/test/person"})
public interface UserHttpApiClient {

    @OkHttpClientBuilder
    static OkHttpClient.Builder okhttpClientBuilder() {
        return new OkHttpClient.Builder()
                .connectTimeout(2, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.SECONDS);

    }

    @GET("/users")
    List<User> getUsers();

    @GET("/user/{name}")
    User getUserByName(@Path("name") String name);

    @POST("/user")
    @FormUrlEncoded
    Integer addUser(@FieldMap Map<String,Object> params);

    @DELETE("/user/{id}")
    Integer deleteUserById(@Path("id") Long id);

    @PUT("/user")
    Integer updateUser(@Body User user);

    @GET("/user/page")
    PageInfo getUserPage(@Query("pageNum") Integer pageNum, @Query("pageSize") Integer pageSize);
}